<?php

namespace app\expert\logic;

use think\facade\Db;
use util\Excel;
use think\exception\ValidateException;

class ExpertInfoLogic extends BasicLogic
{

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $limit = $params['limit'] ?? 10;
        $where = [];
        //店铺
        $shop_id = $params['shop_id'] ?? "";
        if(!empty($shop_id)){
            $where[] = ['shop_id', '=', $shop_id];
        }
        //达人名称
        $expert_name = $params['expert_name'] ?? "";
        if(!empty($expert_name)){
            $where[] = ['expert_name', 'like', "%{$expert_name}%"];
        }
        //合作平台
        $platform_id = $params['platform_id'] ?? "";
        if(!empty($platform_id)){
            $where[] = ['platform_id', '=', $platform_id];
        }
        //合作状态
        $cooperate_status = $params['cooperate_status'] ?? "";
        if(!empty($cooperate_status)){
            $where[] = ['cooperate_status', '=', $cooperate_status];
        }
        //审核结果
        $status = $params['status'] ?? "";
        if(!empty($status)){
            $where[] = ['status', '=', $status];
        }
        //达人名字
        $kol_family_name = $params['kol_family_name'] ?? "";
        if(!empty($kol_family_name)){
            $where[] = ['kol_family_name', 'like', "%{$kol_family_name}%"];
        }
        //是否优质达人
        $excellent = $params['excellent'] ?? "";
        if(!empty($excellent)){
            $where[] = ['excellent', '=', $excellent];
        }
        //建联人
        $admin_id = $params['admin_id'] ?? "";
        if(!empty($admin_id)){
            $where[] = ['admin_id', '=', $admin_id];
        }
        
        $list = Db::name('tik_kol')
            ->where($where)
            ->order('update_time desc')
            ->paginate($limit);
        $lists = $list->items();
        
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $where = [];
        //店铺
        $shop_id = $params['shop_id'] ?? "";
        if(!empty($shop_id)){
            $where[] = ['shop_id', '=', $shop_id];
        }
        //达人名称
        $expert_name = $params['expert_name'] ?? "";
        if(!empty($expert_name)){
            $where[] = ['expert_name', 'like', "%{$expert_name}%"];
        }
        //合作平台
        $platform_id = $params['platform_id'] ?? "";
        if(!empty($platform_id)){
            $where[] = ['platform_id', '=', $platform_id];
        }
        //合作状态
        $cooperate_status = $params['cooperate_status'] ?? "";
        if(!empty($cooperate_status)){
            $where[] = ['cooperate_status', '=', $cooperate_status];
        }
        //审核结果
        $status = $params['status'] ?? "";
        if(!empty($status)){
            $where[] = ['status', '=', $status];
        }
        //达人名字
        $kol_family_name = $params['kol_family_name'] ?? "";
        if(!empty($kol_family_name)){
            $where[] = ['kol_family_name', 'like', "%{$kol_family_name}%"];
        }
        //是否优质达人
        $excellent = $params['excellent'] ?? "";
        if(!empty($excellent)){
            $where[] = ['excellent', '=', $excellent];
        }
        //建联人
        $admin_id = $params['admin_id'] ?? "";
        if(!empty($admin_id)){
            $where[] = ['admin_id', '=', $admin_id];
        }
        
        $lists = Db::name('tik_kol')
            ->where($where)
            ->order('update_time desc')
            ->select()->toarray();

        $header = [];
        $header[] = ['店铺id', 'shop_id'];
        $header[] = ['达人名称', 'expert_name'];
        $header[] = ['达人合创码', 'creation_code'];
        $header[] = ['合作平台', 'platform_id'];
        $header[] = ['合作状态', 'cooperate_status'];
        $header[] = ['付费金额', 'payment_price'];
        $header[] = ['邮箱联系方式', 'email'];
        $header[] = ['达人名字', 'kol_family_name'];
        $header[] = ['达人电话', 'kol_phone'];
        $header[] = ['达人国家', 'country'];

        $fileName = "export_ExpertInfo" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }
    
    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;
        
        try {
            
            $result = false;
            // $result = Db::name('advertising')->insert($insert);

            if ($result !== false) {
                return json(DefaultResponse(200, true, '添加成功!', null));
            } else {
                return json(DefaultResponse(201, false, '添加失败!', null));
            }
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '添加失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($id)
    {
        $row = Db::name('tik_kol')
            ->where('id',$id)
            ->find();
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        if (empty($id)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            $result = true;

            if ($result) {
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));
    }


    public function confirm($data = [])
    {
        return json(DefaultResponse(200, false, '操作成功!', null));
    }


}